<?php

require_once ('library/Rest/Controller/Action.php');


class Rest_Controller_Action_Scaffold extends Rest_Controller_Action
{
	
	/**
	 * The class name of the model this controller represents
	 *
	 * @var string
	 */
	protected $_modelName;
	
	/**
	 * The DB Table class type this controller handles
	 *
	 * @var Zend_Db_Table
	 */
	protected $model;
	
	/**
	 * List the items, filter by the params passed in
	 */
	public function listAction()
	{
		if (!$this->isValidAction()) {
			return;
		}
		
		$where = $this->getWhereClause();
		$this->view->data = $this->model->fetchAll($where)->toArray();
	}
	
	public function viewAction()
	{
		if (!$this->isValidAction())
			return;
		$key = $this->_getParam('key');
		list($this->view->data) = $this->model->find($key)->toArray();
	}
	
	public function createAction()
	{
		if (!$this->isValidAction())
			return;
		$data = $this->getData();
		unset($data['submit']);
//		$form = $this->model->getForm();
		
//		if ($form->isValid($data)) {
			$key = $this->model->insert($data);
			$items = $this->model->find($key)->toArray();
			$this->view->data = empty($items) ? null : $items[0];
//		} else {
//			throw new Exception("Form errors: " . $form->getErrorMessages());
//		}
	}
	
	public function updateAction()
	{
		if (!$this->isValidAction())
			return;
		$key = $this->_getParam('key');
		
		if (!$key)
			return;
		
		$data = $this->getData();
		unset($data['submit']);
//		$form = $this->model->getForm();
		
//		if ($form->isValidPartial($data)) {
			$where = $this->model->getAdapter()->quoteInto('id = ?', $key);
			$this->model->update($data, $where);
			$items = $this->model->find($key)->toArray();
			$this->view->data = empty($items) ? null : $items[0];
//		} else {
//			$this->view->form = $form;
//    		return $this->render('edit');
//		}
		
		if ($this->_helper->contextSwitch->getCurrentContext() == null)
			$this->_redirect('/tests/' . $this->item['id']);
	}
	
	public function deleteAction()
	{
		if (!$this->isValidAction())
			return;
		$key = $this->_getParam('key');
		$where = $this->model->getAdapter()->quoteInto('id = ?', $key);
		$this->model->delete($where);
	}
	
	
	/**
	 * Creates a where clause to be used in a select based off the parameters
	 * sent in.
	 * 
	 * @return Zend_Db_Select
	 */
	protected function getWhereClause()
	{
		$select = $this->model->select();
		$params = $this->_getAllParams();
		$row = $this->model->fetchNew(); // empty row
		
		if (isset($row->deleted)) {
			$select->where('deleted = 0');
		}
		
		foreach ($params as $key => $value) {
			if (!is_array($value) && isset($row->$key)) {
				// allow for things like ?start=>2006&&end=<=2008&active=!1
				if ($value[0] == '<' || $value[0] == '>' || $value[0] == '!') {
					if ($value[0] == '!') {
						$sign = '<>';
						$value = substr($value, 1);
					} else {
						$sign = substr($value, 0, $value[1] == '=' ? 2 : 1);
						$value = substr($value, strlen($sign));
					}
					$select->where("$key $sign ?", $value);
				} else {
					$select->where("$key = ?", $value);
				}
			}
		}
		
		return $select;
	}

	public function init()
	{
		parent::init();
		
		// set up the model if it is defined
		if (!$this->_modelName)
			return;
		
		Zend_Loader::loadClass($this->_modelName);
		
		if (class_exists($this->_modelName, true))
			$this->model = new $this->_modelName();
	}
}

